﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace DiaryPlanner
{
    public partial class PlannerMonth : UserControl
    {
        private Planner planner;

        // Sets a DateTime to April 3, 2002 of the Gregorian calendar.
        public DateTime myDT = DateTime.Now;

        // Uses the default calendar of the InvariantCulture.
        public Calendar myCal = CultureInfo.InvariantCulture.Calendar;

        List<Label> monthList = new List<Label>();

        public PlannerMonth()
        {
            InitializeComponent();
            
            initMonth();
        }

        private void initMonth()
        {
            // 모든 월들을 MonthList에 저장
            monthList.Add(null);
            monthList.Add(monthL1);
            monthList.Add(monthL2);
            monthList.Add(monthL3);
            monthList.Add(monthL4);
            monthList.Add(monthL5);
            monthList.Add(monthL6);
            monthList.Add(monthL7);
            monthList.Add(monthL8);
            monthList.Add(monthL9);
            monthList.Add(monthL10);
            monthList.Add(monthL11);
            monthList.Add(monthL12);
            monthList.Add(monthL13);
            monthList.Add(monthL14);
            monthList.Add(monthL15);
            monthList.Add(monthL16);
            monthList.Add(monthL17);
            monthList.Add(monthL18);
            monthList.Add(monthL19);
            monthList.Add(monthL20);
            monthList.Add(monthL21);
            monthList.Add(monthL22);
            monthList.Add(monthL23);
            monthList.Add(monthL24);
            monthList.Add(monthL25);
            monthList.Add(monthL26);
            monthList.Add(monthL27);
            monthList.Add(monthL28);
            monthList.Add(monthL29);
            monthList.Add(monthL30);
            monthList.Add(monthL31);
            monthList.Add(monthL32);
            monthList.Add(monthL33);
            monthList.Add(monthL34);
            monthList.Add(monthL35);
            monthList.Add(monthL36);
            monthList.Add(monthL37);
            monthList.Add(monthL38);
            monthList.Add(monthL39);
            monthList.Add(monthL40);
            monthList.Add(monthL41);
            monthList.Add(monthL42);

            //클릭 이벤트 핸들러 등록
            for (int i = 1; i < monthList.Count; i++)
                monthList[i].Click += new System.EventHandler(this.month_Click);

            //더블 클릭 이벤트 핸들러 등록
            for (int i = 1; i < monthList.Count; i++)
                monthList[i].DoubleClick += new System.EventHandler(this.month_DoubleClick);

            //해당 월에 일값을 설정
            viewMonth();
        }

        public void viewMonth()
        {
            for (int i = 1; i < monthList.Count; i++)
                monthList[i].Text = "";

            int lastDay = getLastDay();
            int firstweek = getFirstWeek();

            for (int i = 1; i <= lastDay; i++)
                monthList[i + firstweek].Text = i.ToString();
        }

        private int getLastDay()
        {
            int lastDay, tempMonth;
            DateTime tempDT = myDT;

            tempMonth = tempDT.Month;

            for (int i = 0; ; i++)
            {
                tempDT = myCal.AddDays(tempDT, 1);
                if (tempMonth != tempDT.Month)
                    break;
            }
            tempDT = myCal.AddDays(tempDT, -1);
            lastDay = tempDT.Day;

            return lastDay;
        }

        public int getFirstWeek()
        {
            int firstWeek, tempMonth;
            DateTime tempDT = myDT;

            tempMonth = tempDT.Month;

            for (int i = 0; ; i++)
            {
                tempDT = myCal.AddDays(tempDT, -1);
                if (tempMonth != tempDT.Month)
                    break;
            }
            tempDT = myCal.AddDays(tempDT, +1);

            firstWeek = (int)tempDT.DayOfWeek;

            return firstWeek;
        }

        private void month_Click(object sender, EventArgs e)
        {
            Label label = (Label)sender;

            if (label.Text == "") return;

            for (int i = 1; i < monthList.Count; i++)
                monthList[i].BackColor = System.Drawing.Color.White;

            label.BackColor = System.Drawing.Color.Blue;

            planner.monthCalendar1.SelectionStart = planner.monthCalendar1.SelectionStart.AddDays(int.Parse(label.Text) - planner.monthCalendar1.SelectionStart.Day);
            planner.monthCalendar1.SelectionEnd = planner.monthCalendar1.SelectionStart.AddDays(int.Parse(label.Text) - planner.monthCalendar1.SelectionStart.Day);
        }

        private void month_DoubleClick(object sender, EventArgs e)
        {
            Label label = (Label)sender;

            if (label.Text == "") return;

            PlannerEdit PlannerEditObject = new PlannerEdit();
            PlannerEditObject.initPlannerEdit(planner);
            PlannerEditObject.ShowDialog();
        }

        public void month_Click(object sender)
        {
            month_Click(sender, null);
        }

        public void month_DoubleClick(object sender)
        {
            month_DoubleClick(sender, null);
        }

        public Label getMonthList(int index)
        {
            return monthList[index];
        }

        public void savePlanner(Planner planner)
        {
            this.planner = planner;
        }
    }
}
